package start.spring.basic.flowable.controller.form;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.security.annotation.RequiresPermissions;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import start.spring.basic.flowable.common.SysConstant;
import start.spring.basic.flowable.controller.BaseController;
import start.spring.basic.flowable.entity.WfExternalForm;
import start.spring.basic.flowable.service.form.IWfExternalFormService;
import start.spring.basic.log.ServiceLog;
import start.spring.basic.page.PageQuery;
import start.spring.basic.page.PageResult;
import start.spring.basic.response.ResponseData;
import start.spring.basic.response.ResponsePage;

/**
 * 
 * @author xuyy
 * @date 2023-08-24
 *
 */
@RestController
@RequestMapping("/form")
@Api(tags = "表单管理相关接口")
public class WfExternalFormController extends BaseController {
	
    @Autowired
    private IWfExternalFormService wfExternalFormService;
	
    @ServiceLog(businessType = "查询表单管理列表", systemType = SysConstant.SYSTEM_TYPE, recordResult = false, userId = "")
    @RequiresPermissions("workflow:form:query")
    @PostMapping("/list")
    @ApiOperation(value = "查询表单管理列表", notes = "分页查询")
    public ResponsePage<WfExternalForm> list(PageQuery pageQuery, @RequestBody WfExternalForm wfExternalForm) {
    	return super.returnResponse(this.wfExternalFormService.selectWfExternalFormPageList(pageQuery, wfExternalForm));
    }
    
    @ServiceLog(businessType = "查询表单管理详情", systemType = SysConstant.SYSTEM_TYPE, recordResult = true, userId = "")
    @RequiresPermissions("workflow:form:query")
    @PostMapping("/detail")
    @ApiOperation(value = "查询表单管理详情", notes = "单条记录详情查询")
    public ResponseData<WfExternalForm> detail(@RequestBody WfExternalForm wfExternalForm) {
    	PageResult<WfExternalForm> pageResult = this.wfExternalFormService.selectWfExternalFormPageList(new PageQuery(), wfExternalForm);
    	if(pageResult.getRecords()!=null&&!pageResult.getRecords().isEmpty()) {
    		WfExternalForm detail = pageResult.getRecords().get(0);
    		return ResponseData.successData(detail);
    	}else {
    		return ResponseData.errorMsg("未查询到详细信息！");
    	}
    }
    
    @ServiceLog(businessType = "查询表单管理列表", systemType = SysConstant.SYSTEM_TYPE, recordResult = false, userId = "")
    @RequiresPermissions("workflow:form:query")
    @GetMapping("/list/all")
    @ApiOperation(value = "查询表单管理列表", notes = "分页查询")
    public ResponseData<List<WfExternalForm>> listAll() {
    	return ResponseData.successData(this.wfExternalFormService.selectWfExternalFormList(null));
    }
	
    @ServiceLog(businessType = "新增表单管理", systemType = SysConstant.SYSTEM_TYPE, recordResult = true, userId = "")
    @RequiresPermissions("workflow:form:add")
    @PostMapping("/add")
    @ApiOperation(value = "新增表单管理", notes = "")
    public ResponseData<Integer> add(@RequestBody WfExternalForm wfExternalForm) {
        return ResponseData.successData(this.wfExternalFormService.insertWfExternalForm(wfExternalForm));
    }
	
    @ServiceLog(businessType = "修改表单管理", systemType = SysConstant.SYSTEM_TYPE, recordResult = true, userId = "")
    @RequiresPermissions("workflow:form:edit")
    @PostMapping("/edit")
    @ApiOperation(value = "修改表单管理", notes = "")
    public ResponseData<Integer> edit(@RequestBody WfExternalForm wfExternalForm) {
    	return ResponseData.successData(this.wfExternalFormService.updateWfExternalForm(wfExternalForm));
    }
	
    @ServiceLog(businessType = "删除表单管理", systemType = SysConstant.SYSTEM_TYPE, recordResult = true, userId = "")
    @RequiresPermissions("workflow:form:remove")
    @PostMapping("/remove")
    @ApiOperation(value = "删除表单管理", notes = "")
    public ResponseData<Integer> remove(@RequestBody List<String> formIds) {
    	return ResponseData.successData(this.wfExternalFormService.deleteWfExternalFormByFormIds(formIds));
    }
    
    @ServiceLog(businessType = "导出表单管理列表", systemType = SysConstant.SYSTEM_TYPE, recordResult = false, userId = "")
    @RequiresPermissions("workflow:form:export")
    @PostMapping("/export")
    @ApiOperation(value = "导出表单管理列表", notes = "")
    public void export(HttpServletResponse response, WfExternalForm wfExternalForm) {
        List<WfExternalForm> list = this.wfExternalFormService.selectWfExternalFormList(wfExternalForm);
        ExcelUtil<WfExternalForm> util = new ExcelUtil<WfExternalForm>(WfExternalForm.class);
        util.exportExcel(response, list, "表单管理数据");
    }
}
